/**
 * Created with JetBrains PhpStorm.
 * User: kathleen
 * Date: 12-12-18
 * Time: 上午11:38
 * To change this template use File | Settings | File Templates.
 */
$.ready(function(){
    $("#lastRetireDate").datepicker({ dateFormat: "yy-mm-dd" });
    $("#lastWorkDay").datepicker({ dateFormat: "yy-mm-dd" });
    var boolUserName = false;
    $("input[name='handoff_user_name']").autocomplete({
        source: function(request, response) {

            $.ajax({
                url:url_user,
                dataType: "json",
                data: {
                    name: request.term
                },
                success: function(data) {
                    response( $.map( data.data, function( item ) {
                        //                    response( $.map( testdata, function( item ) {
                        return {
                            label:  item.name ,
                            value:  item.name,
                            job_name: item.job_name,
                            user_id:  item.user_id,
                            job_id :item.job_id
                        }
                    }));
                }
            });
        },
        select: function( event, ui ) {
            var user_id = ui.item.user_id;
            var job_id = ui.item.job_id;
            ui.item && $(".job_inherit").val(user_id);
//            ui.item && $(".leader_job").val(job_id);
            boolUserName = true;

        }
    });
    $("input[name='parent_name']").bind("keydown",function(event){
        if((event.keyCode == 13 && positionResult == true)||(event.keyCode == 9 && positionResult == true) || (event.keyCode == 32 && positionResult == true)){
            return;
        }
        boolUserName = false;
    });
    $("input[name='parent_name']").bind("blur",function(){
        if(!boolUserName){
            $(".job_inherit").val("");
//            $(".leader_job").val("");
            $(this).val("");

        }
    });

    var positionResult = false;
    $("input[name='after_user_name']").autocomplete({
        source: function(request, response) {

            $.ajax({
                url:url_user,
                dataType: "json",
                data: {
                    name: request.term
                },
                success: function(data) {
                    response( $.map( data.data, function( item ) {
                        //                    response( $.map( testdata, function( item ) {
                        return {
                            label:  item.name ,
                            value:  item.name,
                            job_name: item.job_name,
                            user_id:  item.user_id,
                            job_id :item.job_id
                        }
                    }));
                }
            });
        },
        select: function( event, ui ) {
            var user_id = ui.item.user_id;
            var job_id = ui.item.job_id;
            ui.item && $(".position_inherit").val(user_id);
//            ui.item && $(".leader_job").val(job_id);
            positionResult = true;

        }
    });
    $("input[name='parent_name']").bind("keydown",function(event){
        if((event.keyCode == 13 && positionResult == true)||(event.keyCode == 9 && positionResult == true) || (event.keyCode == 32 && positionResult == true)){
            return;
        }
        positionResult = false;
    });
    $("input[name='parent_name']").bind("blur",function(){
        if(!positionResult){
            $(".after_user_id").val("");
//            $(".leader_job").val("");
            $(this).val("");

        }
    });
    $(".btn_sub").bind('click',function(){
       var job_inherit = $(".job_inherit").val();
       var after_user_id = $(".position_inherit").val();
        if( $("input[name='after_user_name']").parent().find("em").length == 1){
            after_user_id = true;
        }
        if(!job_inherit){
            $("input[name='handoff_user_name']").focus();
            return false;
        }
        if(!after_user_id){
            $("input[name='after_user_name']").focus();
            return false;
        }

    });



}());